import ScrollViewCtrl from "./ScrollViewCtrl";

const { ccclass, property } = cc._decorator;

@ccclass
export default class rankPanel extends cc.Component {

    @property(cc.Node)
    ScrollView: cc.Node = null;
    _data = null;
    _rankData = null;
    _reTime = 0;

    onLoad() {
        console.log("好友域加载成功");
        //@ts-ignore
        window["wx"].onMessage((data) => {
            console.log(data);
            if (data == this._rankData) return;
            //this.ScrollView.active = false;
            if (data && data.text) {
                this._rankData = data;
                console.log("显示排行Rank data:");
                console.log(data);
                if (!data.myData) return;
                data.myData = JSON.parse(data.myData);
                if (data.myData && data.myData.rankReTime) { this._reTime = data.myData.rankReTime; }
                // if (this._data) {
                //     let index = this._data.findIndex((data1) => { return data1.openid == data.myData.openid })
                //     if (index >= 0) this._data[index].KVDataList[0].value = data.myData.score + "";
                //     this.ScrollView.getComponent(ScrollViewCtrl).init(this._data, { extData: data.myData });
                // }
                this.getWXData(data.text == "friendRank", data.myData);
            }
        });
    }

    getWXData(isFriendRank, data) {
        if (isFriendRank) {
            this.showFriendRank(data);
        }
        else {
            this.showGroupRank(data);
        }
    }

    showFriendRank(data) {
        let self = this;
        const dataList = ["score", "now"];
        //@ts-ignore
        wx.getFriendCloudStorage({
            keyList: dataList,
            success: (res) => {
                console.log("score wx.setUserCloudStorage success");
                console.log(res);
                if (!res || res.data.length <= 0) {
                    console.log("无好友数据");
                    self.showRank(self, "", data);
                    return;
                }
                let newData = res.data.length == 1 ? res.data : res.data.sort(self.sortData);
                self.showRank(self, newData, data);
            },
            fail: (res) => {
                console.log("score wx.setUserCloudStorage fail");
                console.log(res);
            }
        });
    }

    showGroupRank(data) {
        let self = this;
        const dataList = ["score", "now"];
        //@ts-ignore
        wx.getGroupCloudStorage({
            shareTicket: data.shareTicket,
            keyList: dataList,
            success: (res) => {
                console.log("score wx.setUserCloudStorage success");
                console.log(res);
                if (!res || res.data.length <= 0) {
                    res = {};
                    self.showRank(self, "", data);
                    return;
                }
                let newData = res.data.length == 1 ? res.data : res.data.sort(self.sortData);
                self.showRank(self, newData, data);
            },
            fail: (res) => {
                console.log("score wx.getGroupCloudStorage fail");
                console.log(res);
            }
        });
    }

    sortData(a, b) {
        var scoreA = (a.KVDataList)[0].value;
        var scoreB = (b.KVDataList)[0].value;
        if (Number(scoreA) > Number(scoreB)) {
            return -1;
        } else if (Number(scoreA) < Number(scoreB)) {
            return 1
        } else {
            return 0;
        }
    }

    isSameWeek(timeBefore) {
        let timeAfter = (this._reTime) || Date.now();
        let oneDayLong = 24 * 60 * 60 * 1000; // 一天的毫秒数
        let c_day = new Date(timeAfter).getDay() || 7; // 当前时间的星期几
        let m_time = timeAfter - (c_day - 1) * oneDayLong; // 当前周一的毫秒时间
        return timeBefore >= m_time;
    }

    showRank(self, data, plData) {
        let newData = []
        if (data[0] && data[0].KVDataList && data[0].KVDataList.length > 0) {
            console.log("显示好友排行");
            for (let i = 0; i < data.length; i++) {
                const element = data[i];
                console.log("是否是本周数据", Date.now(), this.isSameWeek(element.KVDataList[1].value));
                if (element.KVDataList[0].value != 0 && element.KVDataList[0].value != "0" && element.KVDataList[1].key == "now" && this.isSameWeek(element.KVDataList[1].value)) {
                    newData.push(element);
                }
            }
            console.log("newData");
            console.log(newData);
            if (newData.length <= 0) return;
        }
        else {
            console.log("无好友排行，显示自己数据");
            if (plData.score == 0) return;
        }
        self.ScrollView.getComponent(ScrollViewCtrl).init(newData, { extData: plData });
        self.ScrollView.active = true;
        this._data = newData;
    }
}
